package com.filesearcher;

import java.awt.Color;
import java.io.File;

public class SearchThread extends Thread {

	private ResultsTab newTab;
	private StringBuffer results;
	private Grep myGrep;
	private File searchFile;
	private String searchText;
	private GrepSearchUI grepSearchUIScreen;
	
	private boolean killMe = false;
	
	public SearchThread( String text, File file, boolean saveResults, File saveFileName, boolean excludeWords, int maxResultLines, boolean showLineNumbers, boolean ignoreCase, GrepSearchUI gui )
	{
		this.setName( "Search-Thread:" + text );
		
		searchFile = file;
       	searchText = text;
       	newTab = new ResultsTab( searchText, this, gui );
		myGrep = new Grep(saveResults, saveFileName, excludeWords, showLineNumbers, ignoreCase, maxResultLines);
       	results = new StringBuffer(); 
       	grepSearchUIScreen = gui;
       	
       	newTab.showLineNumbersSelected = grepSearchUIScreen.showLineNumbers.isSelected();
       	newTab.excludeWordsSelected = grepSearchUIScreen.exculdeContaining.isSelected();
       	newTab.ignoreCaseSelected = grepSearchUIScreen.ignoreCase.isSelected();
       	
       	newTab.showProgressBar();
       	newTab.setCurrentMessageText( "Search in progress" );
       	
       	grepSearchUIScreen.addNewTab(newTab);
       	newTab.setSearchInProgress( true );
       	
       	grepSearchUIScreen.setTabTitle( newTab );
	}
	
	public void run()
	{
    	boolean maxResultsFound = false;
    	
		results = myGrep.searchFile(searchFile, searchText);
    	
    	if ( myGrep.numOfResults == myGrep.maxResultLines )
    	{
    		maxResultsFound = true;
    	}

    	int tabNumber = GrepSearchUI.tabPane.getTabCount();
		
    	if ( results.length() == 0 ) 
        {
    		newTab.setSearchInProgress( false );
    		grepSearchUIScreen.setTabTitle( newTab );
    		
    		newTab.setMessageColor( Color.RED );
    		newTab.setCurrentMessageText( "There were no matching results for your search" );
    		
    		if ( tabNumber == GrepSearchUI.tabPane.getSelectedIndex() + 1 )
    		{
    			grepSearchUIScreen.message.setText( newTab.getCurrentMessageText() );
    			grepSearchUIScreen.message.setForeground( newTab.getMessageColor() );
    		}
    		
    		newTab.setText("No matching results found");
    		
    		//lets also set the results string to contain the correct text
    		//we want written to disk
    		results.append("No matching results found");
    		
    		newTab.showResultsTextArea();
        }
        else
        {
            String messageText = new String();

        	if ( maxResultsFound )
        		messageText = "MAX ";
        	
        	messageText += "Results found: " + myGrep.numOfResults + " matching lines ";
        	
            newTab.setCurrentMessageText( messageText );  
        	newTab.setMessageColor( new Color(0, 100, 0) );
            
            Runtime.getRuntime().gc();
            
            try
            {            	
//                byte[] data;
                
//        		try 
//        		{
        			//data = StringCompress.compress( results.toString() );
//        			grepSearchUIScreen.writeTextToDisk( newTab );
//        		} catch (IOException e) {
//        			// TODO Auto-generated catch block
//        			e.printStackTrace();
//        		}
               	
        		//newTab.setTabIndexNumber( tabNumber );
        		newTab.setDoubleBuffered( true );
               	
               	if ( tabNumber != 0)
               	{
               		newTab.setSearchInProgress( false );
               		grepSearchUIScreen.setTabTitle( newTab );
               		//newTab.setText( results.toString() );
               		newTab.showResultsTextArea();               		               	
               	}
               	
               	if ( tabNumber == 1 )
               	{
               		newTab.setText( results.toString() );
               		grepSearchUIScreen.message.setText( newTab.getCurrentMessageText() );
               		grepSearchUIScreen.message.setForeground( newTab.getMessageColor() );
               	}               		
            }
            catch ( OutOfMemoryError oom )
            {
            	Runtime.getRuntime().gc();
            	newTab.setSearchInProgress( false );
            	newTab.setMessageColor( Color.RED );
            	newTab.setCurrentMessageText( "OutOfMemoryError caught, results can not be displayed!" );
            }

           	Runtime.getRuntime().gc();
        }
    	
    	//always want to write the results string to disk
       	grepSearchUIScreen.writeTextToDisk( results.toString() , Integer.toString( newTab.hashCode() ) );

       	Runtime.getRuntime().gc();
       	
       	//this thread has completed remove it from the thread list
       	GrepSearchUI.removeSearchThread( this );
	}
	
	public ResultsTab getResultsTab()
	{
		return newTab;
	}
	
	public void terminate()
	{
		killMe = true;
	}
}
